package org.rency.mq.kafka.core;

import org.apache.kafka.common.serialization.*;
import org.apache.kafka.common.utils.Bytes;

import java.nio.ByteBuffer;

/**
 * 序列/反序列化类型
 *
 * @author: Created by user_rcy@163.com on 2016/10/14.
 * @version: V1.0.0
 * @since JDK 1.8
 */
public enum SerializerKind {

    STRING(1,StringSerializer.class,StringDeserializer.class,String.class),
    INTEGER(2,IntegerSerializer.class,IntegerDeserializer.class,Integer.class),
    LONG(3,LongSerializer.class,LongDeserializer.class,Long.class),
    DOUBLE(4,DoubleSerializer.class,DoubleDeserializer.class,Double.class),
    BYTES(5,BytesSerializer.class,BytesDeserializer.class, Bytes.class),
    BYTE_BUFFER(6,ByteBufferSerializer.class,ByteBufferDeserializer.class, ByteBuffer.class),
    BYTE_ARRAY(7,ByteArraySerializer.class,ByteArrayDeserializer.class, byte[].class),
    ;

    private int code;

    private Class<?> serializer;

    private Class<?> deserializer;

    private Class<?> clazz;

    SerializerKind(int code, Class<?> keySerializer, Class<?> keyDeserializer, Class<?> clazz){
        this.code = code;
        this.serializer = keySerializer;
        this.deserializer = keyDeserializer;
        this.clazz = clazz;
    }

    public static SerializerKind get(int code){
        if(code <= 0){
            return null;
        }
        for(SerializerKind kind : SerializerKind.values()){
            if(code == kind.getCode()){
                return kind;
            }
        }
        return null;
    }

    public int getCode() {
        return code;
    }

    public Class<?> getSerializer() {
        return serializer;
    }

    public Class<?> getDeserializer() {
        return deserializer;
    }

    public Class<?> getClazz() {
        return clazz;
    }

    public static SerializerKind get(Class<?> clazz){
        if(clazz == null){
            return SerializerKind.STRING;
        }
        for(SerializerKind kind : SerializerKind.values()){
            if(clazz == kind.getClazz()){
                return kind;
            }
        }
        return null;
    }
}